package com.shunhe.storage.ui.repo;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectChangeListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.ruitu.arad.base.BaseActivity;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.arad.util.ToastUtils;
import com.ruitu.router_module.Config;
import com.ruitu.router_module.Logg;
import com.ruitu.router_module.support.view.LinearItemDecoration;
import com.shunhe.storage.R;
import com.shunhe.storage.dao.model.InOutRepoRecord;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

// 出入库记录
public class InOutRepoRecordActivity extends BaseActivity {
    private TextView tv_cust_date, tv_today, tv_this_week, tv_this_month, tv_this_year;
    private TextView tv_in_repo, tv_out_repo;// 入库,出库
    private RecyclerView rcv_list;// 出入库列表

    private InOutRepoAdapter adapter;
    private List<InOutRepoRecord> recordList = new ArrayList<>();
    private String type = "1";

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_in_out_repo_record;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tv_in_repo = findViewById(R.id.tv_in_repo);
        tv_out_repo = findViewById(R.id.tv_out_repo);
        rcv_list = findViewById(R.id.rcv_list);
        tv_cust_date = findViewById(R.id.tv_cust_date);
        tv_today = findViewById(R.id.tv_today);
        tv_this_week = findViewById(R.id.tv_this_week);
        tv_this_month = findViewById(R.id.tv_this_month);
        tv_this_year = findViewById(R.id.tv_this_year);

        setHeadTitle("出入库记录");

        LinearLayoutManager manager = new LinearLayoutManager(this);
        adapter = new InOutRepoAdapter(this);
        LinearItemDecoration divider = new LinearItemDecoration.Builder(this)
                .setSpan(R.dimen.deco_10_dp)
                .setColorResource(R.color.divider_list)
                .setShowLastLine(true)
                .build();
        rcv_list.addItemDecoration(divider);
        rcv_list.setLayoutManager(manager);
        rcv_list.setAdapter(adapter);
        for (int i = 0; i < 8; i++) {
            recordList.add(new InOutRepoRecord());
        }
        adapter.setDataList(recordList);

        setInOutType(tv_in_repo, tv_out_repo);

        setListeners();

    }

    private void setListeners() {
        setOnClickListener(tv_in_repo, tv_out_repo, tv_cust_date, tv_today, tv_this_week, tv_this_month, tv_this_year);
        adapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                if (TextUtils.equals("1", type)) {// 入库记录
                    startActivity(InRepoDetailActivity.class);
                } else {// 出库记录

                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == tv_cust_date) {// 自定义
            isSelStartDate = true;// 修改状态为选择开始日期
            initDatePicker();
            pvTime.show();
        }
        if (v == tv_in_repo) {// 切换入库
            type = "1";
            setInOutType(tv_in_repo, tv_out_repo);
            ToastUtils.showShort("入库记录");
        }
        if (v == tv_out_repo) {// 切换出库
            type = "2";
            setInOutType(tv_out_repo, tv_in_repo);
            ToastUtils.showShort("出库记录");
        }
        if (v == tv_today) {
            ToastUtils.showShort("今日");
        }
        if (v == tv_this_week) {
            ToastUtils.showShort("本周");
        }
        if (v == tv_this_month) {
            ToastUtils.showShort("本月");
        }
        if (v == tv_this_year) {
            ToastUtils.showShort("本年");
        }
    }

    private boolean isSelStartDate = true;// 是否选择开始日期
    private String startDate = "";// 记录选择的开始日期
    private String endDate = "";// 记录选择的结束日期
    private TimePickerView pvTime;

    private void initDatePicker() {//Dialog 模式下，在底部弹出
        TimePickerBuilder builder = new TimePickerBuilder(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                SimpleDateFormat format = new SimpleDateFormat(Config.DATE_FORMAT_YMD);
                if (isSelStartDate) {// 选择开始日期
                    startDate = format.format(date);
                    isSelStartDate = false;// 修改状态为选择结束日期
                    initDatePicker();
                    pvTime.show();
                } else {
                    endDate = format.format(date);
                    isSelStartDate = true;// 修改状态为选择开始日期
                    tv_cust_date.setText(startDate + "至" + endDate);
                }
            }
        })
                .setTimeSelectChangeListener(new OnTimeSelectChangeListener() {
                    @Override
                    public void onTimeSelectChanged(Date date) {
                    }
                })
                .isDialog(true) // 默认设置false ，内部实现将DecorView 作为它的父控件。
                .addOnCancelClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                    }
                })
                .setItemVisibleCount(7) //若设置偶数，实际值会加1（比如设置6，则最大可见条目为7）
                .setLineSpacingMultiplier(2.0f)
                .isAlphaGradient(true)
                .setOutSideCancelable(true);

        builder.setType(new boolean[]{true, true, true, false, false, false});
        if (isSelStartDate) {
            builder.setTitleText("请选择开始日期");
        } else {
            builder.setTitleText("请选择结束日期");
        }

        pvTime = builder.build();

        Dialog mDialog = pvTime.getDialog();
        if (mDialog != null) {
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM);
            params.leftMargin = 0;
            params.rightMargin = 0;
            pvTime.getDialogContainerLayout().setLayoutParams(params);

            Window dialogWindow = mDialog.getWindow();
            if (dialogWindow != null) {
                dialogWindow.setWindowAnimations(com.bigkoo.pickerview.R.style.picker_view_slide_anim);//修改动画样式
                dialogWindow.setGravity(Gravity.BOTTOM);// 改成Bottom,底部显示
                dialogWindow.setDimAmount(0.3f);
            }
        }
    }

    // 切换出入库类型
    private void setInOutType(TextView tv1, TextView tv2) {
        if (tv1.isSelected()) {
            return;
        }
        if (!tv1.isSelected()) {
            tv1.setSelected(true);
        }
        if (tv2.isSelected()) {
            tv2.setSelected(false);
        }
        Logg.i("...type = " + type);
        adapter.setDataList(recordList);
    }

    private class InOutRepoAdapter extends ListBaseAdapter {

        public InOutRepoAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_in_out_repo_record_list;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            TextView tv_code_cust = holder.getView(R.id.tv_code_cust);
            TextView tv_num = holder.getView(R.id.tv_num);
            if (TextUtils.equals("1", type)) {
                tv_num.setText("入库数量:55");
                tv_code_cust.setText("R202013566618");
                tv_code_cust.setTextColor(getResources().getColor(R.color.text_dark_gray));
            } else {
                tv_num.setText("出库数量:50");
                tv_code_cust.setText("张三涵网络科技有限公司");
                tv_code_cust.setTextColor(getResources().getColor(R.color.text_dark_black));
            }
            Logg.i("onBindItemHolder()...type = " + type);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            Logg.i("onBindViewHolder()...");
        }
    }
}